package com.zbdx.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zbdx.bean.School;
import com.zbdx.dao.SchoolDao;
import com.zbdx.dao.impl.SchoolDaoImpl;

/**
 * Servlet implementation class SchoolServlet
 */
@WebServlet("/SchoolServlet")
public class SchoolServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SchoolServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method=request.getParameter("method");
		//System.out.println(method);
		if("add".equals(method)) {
			add(request, response);
		}else if("login".equals(method)){
			login(request, response);
		}else {
			System.out.println("操作有误！");
		}
	
	}

	
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int Schoolid = Integer.parseInt(request.getParameter("Schoolid"));
		String SchoolName = request.getParameter("SchoolName");
		String SchoolPri  = request.getParameter("SchoolPri");
		String SchoolAdd  = request.getParameter("SchoolAdd");
		String SchoolEmail= request.getParameter("SchoolEmail");
		String SchoolTel  =request.getParameter("SchoolTel");
		String SchoolWebsite=request.getParameter("SchoolWebsite");
		School school = new School(Schoolid, SchoolName, SchoolPri, SchoolAdd, SchoolEmail, SchoolTel, SchoolWebsite);
		SchoolDao dao = new SchoolDaoImpl();
		boolean x = dao.addSchool(school);
		if(x) {
			response.sendRedirect(request.getContextPath()+"/schoollogin.jsp");		
		}else {
			response.sendRedirect(request.getContextPath()+"/loginerror.html");
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String SchoolNanme = request.getParameter("SchoolName");
		int Schoolid = Integer.parseInt(request.getParameter("Schoolid")); 
		SchoolDao dao = new SchoolDaoImpl();
		School school = dao.login(Schoolid, SchoolNanme);
		if(school!=null) {
			
			response.sendRedirect(request.getContextPath()+"/mainschool.html");
		}else {
			response.sendRedirect(request.getContextPath()+"/loginerror.html");
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		doGet(request, response);
	}

}
